package com.gragra.sampling.nonproj.tests;
import static org.junit.Assert.*;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.junit.Before;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.nonproj.ConvertHeadedFactory;
import com.gragra.sampling.nonproj.DistHeadModel;
import com.gragra.sampling.nonproj.generators.CreateDependencyCorpus;
import com.gragra.sampling.nonproj.generators.projective.singleroot.CreateGreedySingleRootProjective;
import com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree;
/**
 * 
 * @author Christoph Teichmann
 * 19.10.2013
 */
public class TestDistHeadModel
{/**
	 * 
	 */
	private NonProjDepdTree	tree;
	/**
	 * 
	 */
	private ThreadedUniformAccess	tua;
	/**
	 * 
	 */
	private DistHeadModel	dmv;
	@Before
	public void setUp() throws Exception
	{
		CreateDependencyCorpus cre = new CreateGreedySingleRootProjective(new ConvertHeadedFactory());
		tree = cre.makeTree("this|0 is|1 a|2 sentence|3 .|4",3,0.1);
		tua = new ThreadedUniformAccess(3);
		IntSet tags = new IntOpenHashSet();
		for(int i=0;i<tree.getLength();++i)
		{tags.add(tree.getTag(i));}
		dmv = new DistHeadModel(3, tags, 0.3, 0.5, 1.0);
		tree.initialize(dmv, tua);
	}
	@Test
	public void testMakeLogProbPopulationSamplingStructureInt()
	{
		double d = dmv.makeLogProb(tree, 2);
		assertEquals(dmv.getNumberOfAuxEvals(),0,0.000001);
		assertEquals(d,-37.657173387577856,0.00001);
		assertEquals(dmv.getNumberOfEvaluations(),5.0,0.0001);
		dmv.add(tree, 2);
		assertEquals(dmv.makeLogProb(tree, 2),-24.204598788706846,0.00001);
		dmv.remove(tree, 2);
		assertEquals(dmv.makeLogProb(tree, 2),d,0.00001);
		tree.resample(dmv, 1.0, 0, tua);
		assertTrue(dmv.getNumberOfAuxEvals() > 100.0);
		tree.resample(dmv, 1.0, 0, tua);
		assertTrue(dmv.getNumberOfAuxEvals() > 250.0);
		assertTrue(dmv.getNumberOfEvaluations()>100.0);
		assertEquals(dmv.getLocalLogLike(tree, 0),0.0,0.0001);
	}
}